# -*- coding: utf-8 -*-


from django.shortcuts import render

import json
# Create your views here.
from rest_framework.generics import ListAPIView,CreateAPIView,UpdateAPIView

from rest_framework.views import APIView
from django.views import View
from django.http import JsonResponse
from  django.http import HttpResponse
from django.shortcuts import render
from rest_framework.request import Request
from   rest_framework.response import Response
from  books.serializer import BookSerialzier,HeroInfoSerialzier
from  books.models import BookInfo,HeroInfo

from rest_framework.generics import GenericAPIView
"""
mixin-view 新增的框架

"""

from  rest_framework.mixins import CreateModelMixin,ListModelMixin,\
    RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin


def exam(request):
    return render(request,'index.html')

"""
listModelMinxin 有自己的业务逻辑

"""
class Books(CreateModelMixin, ListModelMixin,GenericAPIView):
    queryset = BookInfo.objects.all()  # 指定当前类视图使用的查询集数据
    serializer_class = BookSerialzier  # 指定当前类视图使用的序列化器

    def get(self, request):
        return self.list(request)

    def post(self, request):
        return self.create(request)


class Book(GenericAPIView):
    def get(self, request):
        book = BookInfo.objects.get(id=1)
        ser = BookSerialzier(book)

        return Response(ser.data)


class BookDRFView(GenericAPIView,UpdateModelMixin,DestroyModelMixin):
    """
        获取单一和更新和删除
    """

    queryset = BookInfo.objects.all()  # 指定当前类视图使用的查询集数据
    serializer_class = BookSerialzier  # 指定当前类视图使用的序列化器

    def put(self, request, pk):
       return self.update(request, pk)

    def delete(self, request, pk):
        return self.destroy(request, pk)